package com.gs.es.utils;

import com.google.common.collect.Lists;
import com.gs.common.tool.consts.UserConst;
import com.gs.common.tool.pojo.AnnotationInfo;
import com.gs.common.tool.pojo.ClassInfo;
import com.gs.common.tool.pojo.ColumnInfo;
import com.gs.common.tool.utils.ClzUtil;
import com.gs.common.tool.utils.FileUtil;
import com.gs.common.tool.utils.LombokUtil;
import com.gs.common.tool.utils.PoJoUtil;
import com.gs.es.consts.ESAnnotationConst;

import java.io.File;
import java.util.List;
import java.util.Map;

public class JavaCodeGenUtil extends com.gs.common.tool.utils.JavaCodeGenUtil {


    /**
     * 根据数据库列信息，生成实体类
     *
     * @param filePath       实体类路径
     * @param esAnnoMap         注解map
     * @param tableName      表名
     * @param packageName    包路径
     * @param columnInfoList 列信息
     * @date 2024/05/17 15:54
     **/
    public static void genPojo(String filePath, Map<String, String> esAnnoMap, String tableName, String pojoName, String packageName, String enableTableSuper, String enableSuper, String superClass, String tableComment, List<ColumnInfo> columnInfoList, String author) {
        tableComment = tableComment + "实体类";
        //类名
        String clzName = String.format("%s.%s", packageName, pojoName);
        //文件全路径
        String file = String.format("%s%s%s", filePath, File.separator, clzName.replace(".", File.separator));
        boolean haveSuperClz = ClzUtil.isHaveSuperClass(enableTableSuper, enableSuper, superClass);
        //lombok类导入
        List<String> thirdClzNameList = LombokUtil.addAnnotationFullName(haveSuperClz);
        //es类注解
        String es_table = esAnnoMap.get(ESAnnotationConst.ES_TABLE);
        String es_field = esAnnoMap.get(ESAnnotationConst.ES_FIELD);
        thirdClzNameList.add(es_table);
        thirdClzNameList.add(es_field);
        //注解集合
        List<AnnotationInfo> annotationList = LombokUtil.addAnnotationShortName(haveSuperClz);
        annotationList.add(new AnnotationInfo(PoJoUtil.getShortName(es_table), Lists.newArrayList(new AnnotationInfo.Field("indexName", tableName, true))));
        if (haveSuperClz) {
            columnInfoList.removeIf(c -> UserConst.ignoreFieldList.contains(c.getColumnName()));
        }
        FileUtil.writeToJavaFile(file, ClzUtil.genClass(annotationList, true, clzName, haveSuperClz ? new ClassInfo(superClass, null) : null, new ClassInfo("java.io.Serializable", null), tableComment, author, ClzUtil.addSerialVersionUID() + ClzUtil.addField(columnInfoList, thirdClzNameList, es_field), thirdClzNameList));
    }
}